﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BISPOS.Repository;
using DevExpress.XtraEditors.Controls;
namespace BISPOS
{
    public partial class CustomerDetailsUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        CustomerRepository customerRepository = new CustomerRepository();
        Customer customer;
        public CustomerDetailsUserControl(Customer _customer)
        {
            InitializeComponent();
            customerBindingSource.DataSource = _customer;
            customer = _customer;
            decimal temp = 0;

            temp = customer.AllCashBuyingCost;
            cashSummaryTextEdit.Text = temp.ToString("n2");
            if (temp <= 0)
                cashSummaryButton.Enabled = false;

            temp = customer.AllCreditBuyingCost;
            creditSummaryTextEdit.Text = temp.ToString("n2");
            if (temp <= 0)
                creditSummaryButton.Enabled = false;

            temp = customer.AllBuyingCost;
            summaryTextEdit.Text = temp.ToString("n2");
            if (temp <= 0)
                summaryTextEdit.Enabled = false;

            temp = customer.AllCreditBuyingCost;
            loanTextEdit.Text = temp.ToString("n2");
            if (temp <= 0)
                loanButton.Enabled = false;
            

            // initialize
            // end
        }

        private void cashSummaryButton_Click(object sender, EventArgs e)
        {
            CustomerInvoicesForm form = new CustomerInvoicesForm(customer.CashBuyingInvoices, "รายการซื้อเงินสด");
            form.Show();
        }

        private void creditSummaryButton_Click(object sender, EventArgs e)
        {
            CustomerInvoicesForm form = new CustomerInvoicesForm(customer.CreditBuyingInvoices, "รายการซื้อเงินเชื่อ");
            form.Show();
        }

        private void allSummaryButton_Click(object sender, EventArgs e)
        {
            CustomerInvoicesForm form = new CustomerInvoicesForm(customer.Invoices.OrderByDescending(x=>x.Date), "รายการซื้อทั้งหมด");
            form.Show();
        }

        private void loanButton_Click(object sender, EventArgs e)
        {
            CustomerInvoicesForm form = new CustomerInvoicesForm(customer.CreditBuyingInvoices, "รายการซื้อเงินเชื่อ");
            form.Show();
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if (editButton.Text == "แก้ไข")
            {
                editButton.Text = "ยกเลิก";
                saveButton.Enabled = true;

                nameTextEdit.Properties.ReadOnly = false;
                sexTextEdit.Properties.ReadOnly = false;
                addressMemoEdit.Properties.ReadOnly = false;
                loanLimit.Properties.ReadOnly = false;

            }
            else
            {
                editButton.Text = "แก้ไข";
                saveButton.Enabled = false;

                nameTextEdit.Properties.ReadOnly = true;
                sexTextEdit.Properties.ReadOnly = true;
                addressMemoEdit.Properties.ReadOnly = true;
                loanLimit.Properties.ReadOnly = true;
                
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                customerRepository.EditCustomer(codeTextEdit.Text, nameTextEdit.Text,
                    sexTextEdit.Text, addressMemoEdit.Text, Convert.ToDecimal(loanLimit.Text));
                MessageBox.Show("บันทึกแล้ว");

                editButton.Text = "แก้ไข";
                saveButton.Enabled = false;

                nameTextEdit.Properties.ReadOnly = true;
                sexTextEdit.Properties.ReadOnly = true;
                addressMemoEdit.Properties.ReadOnly = true;
                loanLimit.Properties.ReadOnly = true;

            }
            catch { MessageBox.Show("เกิดข้อผิดพลาด"); }
        }
    }
}
